const Router = require('@koa/router');
const { Jingdian } = require('../databases');

const jingdianRouter = new Router({
    prefix: '/jingdian'
});

// 获取景点首页
jingdianRouter.get('/', (ctx, next) => {
    ctx.body = {
        code: 200,
        message: '景点首页',
        data: 'Welcome to jingdian API',
    };
});

// 添加景点
jingdianRouter.post('/add', async (ctx, next) => {
    try {
        const { name, desc, category, content, logoRemoteImageUrl } = ctx.request.body;
        
        const currentJingdian = await Jingdian.create({
            name,
            desc,
            category,
            content,
            logoRemoteImageUrl,
        });
        
        ctx.body = {
            code: 200,
            message: '添加景点成功',
            data: currentJingdian,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '添加景点失败',
            error: error.message
        };
    }
});

// 获取所有景点列表
jingdianRouter.get('/list', async (ctx, next) => {
    try {
        const jingdians = await Jingdian.findAll({
            order: [['createAt', 'DESC']]
        });
        
        ctx.body = {
            code: 200,
            message: '获取景点列表成功',
            data: jingdians,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '获取景点列表失败',
            error: error.message
        };
    }
});

// 根据ID获取景点详情
jingdianRouter.get('/:id', async (ctx, next) => {
    try {
        const { id } = ctx.params;
        const jingdian = await Jingdian.findByPk(id);

        if (!jingdian) {
            ctx.body = {
                code: 404,
                message: '景点不存在',
                data: null
            };
            return;
        }

        ctx.body = {
            code: 200,
            message: '获取景点详情成功',
            data: jingdian,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '获取景点详情失败',
            error: error.message
        };
    }
});

// 更新景点信息
jingdianRouter.put('/:id', async (ctx, next) => {
    try {
        const { id } = ctx.params;
        const { name, desc, category, content, logoRemoteImageUrl } = ctx.request.body;

        const jingdian = await Jingdian.findByPk(id);
        if (!jingdian) {
            ctx.body = {
                code: 404,
                message: '景点不存在',
                data: null
            };
            return;
        }

        await jingdian.update({
            name: name || jingdian.name,
            desc: desc || jingdian.desc,
            category: category || jingdian.category,
            content: content || jingdian.content,
            logoRemoteImageUrl: logoRemoteImageUrl || jingdian.logoRemoteImageUrl,
            lastModifyAt: new Date()
        });

        ctx.body = {
            code: 200,
            message: '更新景点成功',
            data: jingdian,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '更新景点失败',
            error: error.message
        };
    }
});

// 删除景点
jingdianRouter.delete('/:id', async (ctx, next) => {
    try {
        const { id } = ctx.params;
        
        const jingdian = await Jingdian.findByPk(id);
        if (!jingdian) {
            ctx.body = {
                code: 404,
                message: '景点不存在',
                data: null
            };
            return;
        }

        await jingdian.destroy();

        ctx.body = {
            code: 200,
            message: '删除景点成功',
            data: null,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '删除景点失败',
            error: error.message
        };
    }
});

// 根据类别获取景点列表
jingdianRouter.get('/category/:category', async (ctx, next) => {
    try {
        const { category } = ctx.params;
        const jingdians = await Jingdian.findAll({
            where: { category },
            order: [['createAt', 'DESC']]
        });

        ctx.body = {
            code: 200,
            message: '获取分类景点列表成功',
            data: jingdians,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '获取分类景点列表失败',
            error: error.message
        };
    }
});

// 搜索景点
jingdianRouter.get('/search/:keyword', async (ctx, next) => {
    try {
        const { keyword } = ctx.params;
        const { Op } = require('sequelize');
        
        const jingdians = await Jingdian.findAll({
            where: {
                [Op.or]: [
                    { name: { [Op.like]: `%${keyword}%` } },
                    { desc: { [Op.like]: `%${keyword}%` } },
                    { content: { [Op.like]: `%${keyword}%` } }
                ]
            },
            order: [['createAt', 'DESC']]
        });

        ctx.body = {
            code: 200,
            message: '搜索景点成功',
            data: jingdians,
        };
    } catch (error) {
        ctx.body = {
            code: 500,
            message: '搜索景点失败',
            error: error.message
        };
    }
});

module.exports = jingdianRouter;


